package com.whfc.fse.manager.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.util.HttpUtil;
import com.whfc.fse.manager.FseCraneSZJDataMgr;
import com.whfc.fse.manager.FseLiftSZJDataMgr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

@Component
public class FseLiftSZJDataMgrImpl implements FseLiftSZJDataMgr {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public final  String URL = "https://sc.uni-ubi.com/sc/api";

    @Override
    public JSONObject getLiftRealData(String deviceKey, Integer offset, Integer recordSize, String startTime, String endTime,String token) {
        String path = "/v1/api/elevator/monitor/query";
        Map<String, String> headers = new HashMap<>();
        headers.put("token",token);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("deviceKey",deviceKey);
        jsonObject1.put("offset",offset);
        jsonObject1.put("recordSize",recordSize);
        jsonObject1.put("startTime",startTime);
        jsonObject1.put("endTime",endTime);
        String s = HttpUtil.doPost(URL+path, JSONObject.toJSONString(jsonObject1),headers);
        logger.info("宇泛拉取升降机实时数据:{}",s);
        JSONObject result = JSONObject.parseObject(s);
        JSONObject data = result.getJSONObject("data");
        JSONArray records = data.getJSONArray("records");
        if (CollectionUtils.isEmpty(records)) {
            return null;
        }
        return (JSONObject)records.get(0);
    }

    @Override
    public JSONArray getLiftWorkData(String deviceKey, Integer offset, Integer recordSize, String startTime, String endTime,String token) {
        String path = "/v1/api/elevator/round/query";
        Map<String, String> headers = new HashMap<>();
        headers.put("token",token);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("deviceKey",deviceKey);
        jsonObject1.put("offset",offset);
        jsonObject1.put("recordSize",recordSize);
        jsonObject1.put("startTime",startTime);
        jsonObject1.put("endTime",endTime);
        String s = HttpUtil.doPost(URL+path, JSONObject.toJSONString(jsonObject1),headers);
        logger.info("宇泛拉取升降机工作数据:{}",s);
        JSONObject result = JSONObject.parseObject(s);
        JSONObject data = result.getJSONObject("data");
        JSONArray records = data.getJSONArray("records");
        return records;
    }

//    @Override
//    public JSONArray getLiftWarnData(String deviceKey, Integer offset, Integer recordSize, String startTime, String endTime,String token) {
//        String path = "/v1/api/tower/alarm/query";
//        Map<String, String> headers = new HashMap<>();
//        headers.put("token",token);
//        JSONObject jsonObject1 = new JSONObject();
//        jsonObject1.put("deviceKey",deviceKey);
//        jsonObject1.put("offset",offset);
//        jsonObject1.put("recordSize",recordSize);
//        jsonObject1.put("startTime",startTime);
//        jsonObject1.put("endTime",endTime);
//        String s = HttpUtil.doPost(URL+path, JSONObject.toJSONString(jsonObject1),headers);
//        logger.info("宇泛拉取升降机报警数据:{}",s);
//        JSONObject result = JSONObject.parseObject(s);
//        JSONObject data = result.getJSONObject("data");
//        JSONArray records = data.getJSONArray("records");
//        return records;
//    }

}
